﻿var Config =
{
	ACTIVITY_FLASH_INSTANCE_NAME:"ActivityFlashMovie"
}
var Core = 
{
	OnIE:(navigator.appName.indexOf("Microsoft") != -1), // Of course it's on IE... right!? \o/
	//
	// Generic Common Methods
	GetObject:function(id)
	{
		return document.getElementById(id);
	},
	CreateDriver:function(ObjectID)
	{
		var DriverId	= Core.Drivers.Items.length;
		var Driver		= Core.Drivers.Items[DriverId] = new Core.Drivers.ActivityDriver(ObjectID);
		
		Driver.Init();
		
		return DriverId;
	},
	Debug:function(MSG)
	{
		return;
		var debuggerDiv  = document.getElementById( "debuggerDiv" );
		if ( debuggerDiv != null )
		{
			if( MSG == undefined )
				MSG = "undefined";
			debuggerDiv.innerHTML += MSG + "<br/>\n";
		}
	},
	GetFlashInstance:function()
	{
		//var obj = (Core.OnIE) ? window[Config.ACTIVITY_FLASH_INSTANCE_NAME] : document[Config.ACTIVITY_FLASH_INSTANCE_NAME];
		var obj =  document[Config.ACTIVITY_FLASH_INSTANCE_NAME];
		//Core.Debug('Core.GetFlashInstance ' + Core.OnIE + ' ' + String(obj) );
		if( obj == undefined ) obj = {};
		return obj;
		/*
		function formSend() 
		{   
		var text = document.htmlForm.sendField.value;   
		getFlashMovie("ExternalInterfaceExample").sendTextToFlash(text);     
		}    
		
		function getTextFromFlash(str) 
		{   
		document.htmlForm.receivedField.value = "From Flash: " + str;   
		return str + " received";  
		}
		*/
	},
	//
	// Activity API Util Methods
	GetUsers:function() {
		return window.external.Users;
	},
	GetUserAt:function(Index) {
		return Core.GetUsers().Item(Index);
	},
	GetMe:function() {
		return Core.GetUsers().Me;
	},
	GetUserCount:function() {
		Core.Debug( '+++' + Core.GetUsers().Count );
		return Core.GetUsers().Count;
	},
	GetChannel:function()
	{
		return window.external.Channel;
	},
	GetChannelData:function()
	{
		return Core.GetChannel().Data;
	},
	SendData:function(Data)
	{
		Core.GetChannel().SendData(Data);
	},
	//
	// Event Handlers
	Handlers:
	{
		Channel_OnRemoteAppLoaded:function()
		{
			Core.Debug('Core.Handlers.Channel_OnRemoteAppLoaded' );
			Core.Drivers.ForEachDriver("OnRemoteAppLoaded");
		},
		Channel_OnRemoteAppClosed:function()
		{
			Core.Debug('Core.Handlers.Channel_OnRemoteAppClosed' );
			Core.Drivers.ForEachDriver("OnRemoteAppClosed");
		},
		Channel_OnDataReceived:function()
		{
			Core.Debug('Core.Handlers.Channel_OnDataReceived >>> ' + Core.GetChannelData() );
			var Data = Core.GetChannelData();
			Core.Drivers.ForEachDriver("OnDataReceived", [Data]);
		},
		Channel_OnDataError:function()
		{
			Core.Debug('Core.Handlers.Channel_OnDataError' );
			Core.Drivers.ForEachDriver("OnDataError");
		},
		Channel_OnTypeChanged:function()
		{
			Core.Debug('Core.Handlers.Channel_OnTypeChanged' );
			Core.Drivers.ForEachDriver("OnTypeChanged");
		},
		Channel_OnAppClose:function()
		{
			Core.Debug('Core.Handlers.Channel_OnAppClose' );
			Core.Drivers.ForEachDriver("OnAppClose");
		}
	},
	Drivers:
	{
		Items:[],
		ForEachDriver:function(MethodName, Arguments)
		{
			if( Arguments === undefined ) Arguments = [];
			
			for( var DriverId in this.Items )
			{
				Core.Drivers.CallDriverMethod(DriverId, MethodName, Arguments);
			}
		},
		CallDriverMethod:function(DriverId, MethodName, Arguments)
		{
			Core.Debug( 'CallDriverMethod ' + MethodName );
			if( !Arguments ) Arguments = [];
			
			var Driver = Core.Drivers.Items[DriverId];
			var Method = Driver[MethodName];
			if( Driver && Method )
			{
				try
				{
					return Method.apply( Driver, Arguments );
				}
				catch(Err)
				{
					Core.Debug( ['Err', Err.message, DriverId, Driver, Method, MethodName,'|',Arguments] );
				}
			}
			return;
		},
		ActivityDriver:function(objectId)
		{
			this.FlashId		= objectId;
			this.FlashReference	= null;
			
			this.Init = function()
			{
				this.FlashReference = (Core.OnIE) ? document[this.FlashId] : window[this.FlashId];
				Core.Debug( String(this.FlashId) + " " + String(this.FlashReference) );
			}
			
			/******************************************************/
			/* CHANNEL Methods
			/******************************************************/
			this.Channel_SendData = function(Data)
			{
				//TODO: add activated/deactivated validation and controls
				Core.SendData(Data);
			}
			
			/******************************************************/
			/* USERS Methods
			/******************************************************/
			this.Users_Count = function()
			{
				//Core.Debug( 'Users_Count ' + Core.GetUserCount() );
				return Core.GetUserCount();
			}
			
			this.Users_GetUserReference = function(OID)
			{
				var User = null;
				switch( OID )
				{
					case 'Me':		User = Core.GetUsers().Me; break;
					case 'Inviter':	User = Core.GetUsers().Inviter; break;
					default:		User = Core.GetUserAt(OID); break;
				}
				return Core.Convert.User2Object(User);
			}
			
			/******************************************************/
			/* EVENTS
			/******************************************************/
			this.OnRemoteAppLoaded	= function(){		if(this.FlashReference) this.FlashReference.DispatchEvent2("REMOTE_APP_LOADED");				}
			this.OnRemoteAppClosed	= function(){		if(this.FlashReference) this.FlashReference.DispatchEvent2("REMOTE_APP_CLOSED");				}
			this.OnDataReceived		= function(Data){	if(this.FlashReference) this.FlashReference.DispatchEvent2("DATA_RECEIVED", {data:Data});		}
			this.OnDataError		= function(){		if(this.FlashReference) this.FlashReference.DispatchEvent2("DATA_ERROR");						}
			this.OnOnTypeChanged	= function(){		if(this.FlashReference) this.FlashReference.DispatchEvent2("ON_TYPE_CHANGED", {type:null});		}
			this.OnAppClose			= function(){		if(this.FlashReference) this.FlashReference.DispatchEvent2("APP_CLOSE");						}
		}
	},
	Convert:{
		User2Object:function(User)
		{
			if( !User ) return null;
			return	{
						EMail:User.EMail,
						GlobalIP:User.GlobalIP,
						LocalIP:User.LocalIP,
						Name:User.Name,
						PUID:User.PUID
					};
		}
	}
}

Channel_OnRemoteAppLoaded	= Core.Handlers.Channel_OnRemoteAppLoaded;
Channel_OnRemoteAppClosed	= Core.Handlers.Channel_OnRemoteAppClosed;
Channel_OnDataReceived		= Core.Handlers.Channel_OnDataReceived;
Channel_OnDataError			= Core.Handlers.Channel_OnDataError;
Channel_OnTypeChanged		= Core.Handlers.Channel_OnTypeChanged;
Channel_OnAppClose			= Core.Handlers.Channel_OnAppClose;




/*
function ActivityDriver(objectId)
{
	this.FlashId		= objectId;
	this.FlashReference	= null;
	
	this.Init = function()
	{
		this.FlashReference = (Core.OnIE) ? document[this.FlashId] : window[this.FlashId];
		//Core.Debug( String(this.FlashReference) + " " +  )
	}
}
*/

//var Driver = new Core.Drivers.ActivityDriver('ActivityFlashMovie');

function Simulate()
{
	var drvierId = Core.CreateDriver("ActivityFlashMovie");
	Core.Debug(drvierId + " * " );
	
	var drvierId = Core.CreateDriver("Flash 2");
	Core.Debug(drvierId + " * " );
	
	
	Core.Handlers.Channel_OnRemoteAppLoaded();
}

//setTimeout( function (){ Core.Debug( Driver + Driver.Init() ) }, 500 );
//setTimeout( Simulate, 500 );















